<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Slides extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'slides';
        $this->load->model('Mslides');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 3, $this->session->userdata('admin_id'));
    }
    
    public function index()
    {
        if(@$this->_per->read != 1 && @$this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['data'] = $this->Mslides->getSlides();
        
        $this->load->library('form_validation');
        //$this->form_validation->set_rules('slides_check_id_1', 'Ảnh slider', 'required');
        //$this->form_validation->set_rules('url_slides_id_1', 'Link slider', 'required');
		$this->form_validation->set_rules('count_slides', 'Số slide', 'required');
		
		
        $this->data['listCoupon'] = $this->Mslides->listCoupon();
		$this->data['listIncentive'] = $this->Mslides->listIncentive();
        if($this->form_validation->run() === TRUE)
        {            
            $count_slides = $this->getParamInt('count_slides');
			//die(''.$count_slides);
            $path = 'images/slides/';
            @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
            $arr_upload = array();
            for($i = 1;$i <= $count_slides; $i++)
            {
                $data_image = '';
                $dataimagecheck = $this->getParamString('slides_check_id_'.$i);
                
                $verify = $this->getParamString('slides_verify_id_'.$i);
                if(!in_array($verify, array(null, '')))
                {
                    if($verify != $dataimagecheck)
                    {
                        @unlink(PROJECT_PATH.'/data/'.$verify);
                    }
                }
                $arr_upload[$i]['id'] = $i;
                
				
				$cp_id = $this->getParamInt('coupon_id'.$i);
				$ict_id = $this->getParamInt('ict_id'.$i);
				if ( !in_array($cp_id,array('',null,'0')) ){
					$arr_upload[$i]['coupon_id'] = (string)$this->getParamInt('coupon_id'.$i);
					$arr_upload[$i]['url'] = '';
				}else{
					if ( !in_array($ict_id,array('',null,'0')) ){
						$arr_upload[$i]['coupon_id'] = '-'.$this->getParamInt('ict_id'.$i);
						$arr_upload[$i]['url'] = '';
					}else{
						$arr_upload[$i]['coupon_id'] = '';
						$arr_upload[$i]['url'] = $this->getParamString('url_slides_id_'.$i);
					}
				}
				
                if($dataimagecheck != 1){
                    $arr_upload[$i]['image'] = $dataimagecheck;
                }else{
                    /*$dataimage = $this->getParamString('slides_id_'.$i);
                    if(!in_array($dataimage, array(null, '', '0')))
                    {
                        $timenow = time().rand(0,9999);
                        $nameimagess = date('Ym').'-slides-'.$timenow.'.jpg';
                        $paths = PROJECT_PATH.'/data/images/slides/';
                        $arr_exp = array('[removed]', '[removed');
                        $exp = str_replace($arr_exp,'',$dataimage);
                        $this->savefile($exp,$paths,$nameimagess);
                        $arr_upload[$i]['image'] = $path.$nameimagess;
                    }else{
                        $arr_upload[$i]['image'] = '';
                    } */
                    
                    $path1 = 'images/slides/';
        			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
        			$config['upload_path'] = '../data/'.$path1;
        			$config['allowed_types'] = 'gif|jpg|png';
        			$config['max_size']	= '2000';
        			$config['max_width']  = '10240';
        			$config['max_height']  = '7680';
        			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
        			$this->load->library('upload', $config);
        			if( !$this->upload->do_upload('slides_uploads_'.$i)){
        				$arr_upload[$i]['image'] = '';
        			}else{
        				$data_img = $this->upload->data();
        				$arr_upload[$i]['image'] = $path1.$data_img['file_name'];
        			}	
                }
            }
            
            $arr = array();
            $arr_n = 0;
            foreach($arr_upload AS $el=>$le)
            {
                if($le['image'] != '')
               {
                   $arr_n = $arr_n + 1;
                   $arr[$arr_n] = $le;
               }		   
            }
            //echo "<pre>";
            //die(print_r($arr));
            $this->data['data'] = $this->Mslides->updateSlides($arr);
        }
        
        $this->data['action'] = 'slides';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleteslidesajax()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $path = $this->getParamString('path');
        if($path != 1)
        {
            @unlink(PROJECT_PATH.'/data/'.$path);
        }
    }
	
	public function ajaxgetlistcoupon(){
		die($this->Mslides->ajaxgetlistcoupon($this->getParamInt('ids')));
	}
	
}